// pages/link/link.js
var md5 = require("../../utils/md5.js");
const util = require('../../utils/util.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    "url":''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    if(options.name){
      wx.setNavigationBarTitle({
        title: options.name,
      })
    }
    if(options.url.indexOf("https://punch.superlib.com") > -1 || options.url.indexOf("https://entry-reg.libsp.com") > -1){
      //张鹏负责的活动  屏蔽右上角分享功能
    wx.hideShareMenu();
    }
    var _url = "";
    // if (options.url == "https://punch.superlib.com/"){
    if (options.url.indexOf("https://punch.superlib.com/api/deptac/web") > -1){
      _url = decodeURIComponent(options.url+"?activityId=" + options.activityId + "&uid=" + options.uid + "&dxfid=" + options.dxfid + "&from=cx&fid=" + app.globalData.fid)
     
    }else if(options.url == "https://punch.superlib.com/"){
      _url = decodeURIComponent(options.url+"?activityId=" + options.activityId + "&uid=" + options.uid + "&dxfid=" + options.dxfid + "&from=cx&fid=" + app.globalData.fid)
    }else if(options.url.indexOf("https://entry-reg.libsp.com") > -1 ){
      //入馆预约
      _url = decodeURIComponent(options.url+"?fid=" + options.fid + "&uid=" + options.uid + "&enc=" + options.enc);
    }else if (options.url.indexOf("https://cxtsccs.chaoxing.com") > -1 ){
      _url = decodeURIComponent(options.url + "?uid=" + options.uid + "&phone=" + options.phone + "&realname=" + options.phone)
    }else if( options.url.indexOf("https://videoshequ3.chaoxing.com") > -1){
      var uid = wx.getStorageSync('cxid') == undefined ? "" : wx.getStorageSync('cxid');
      _url = decodeURIComponent(options.url + "?activityId=" + options.activityId + "&type=" + options.type + "&uid=" + uid + "&fid=" + options.fid);
    } else if (
      options.url.indexOf("https://pro.superlib.com") > -1 &&
      !options.sid &&
      !options.tid
    ) {
      _url = decodeURIComponent(options.url + "?vid=" + options.vid);
    } else if (
      options.url.indexOf("https://pro.superlib.com") > -1 &&
      options.sid
    ) {
     
      var datetime = new Date().Format('yyyyMMddhh');
      var cxid = wx.getStorageSync('cxid') == undefined ? "" : wx.getStorageSync('cxid');
      console.log(cxid)
      //判断是否登录
      if(cxid == undefined || cxid ==""){
        wx.showModal({
          title: '提示',
          content: '现在去登录吗？',
          success(res) {
            if (res.confirm) {
              console.log("去登陆")
              wx.switchTab({
                url: '/pages/mine/mine',
              })
            } else if (res.cancel) {
             wx.navigateBack({
               
             })
            }
          }
        })
      }else{
        var enc = md5.md5('[' + datetime + '][' + app.globalData.fid + '][' + options.sid + '][video_play][' + cxid + '][' + app.globalData.pro_superlib + ']').toLowerCase();

        _url = decodeURIComponent(
          options.url +
          "?datetime=" +
          datetime +
          "&fid=" + app.globalData.fid + "&sid=" + options.sid + "&uid=" + cxid + "&enc=" +
          enc
        );
      }
      
    } else if (
      options.url.indexOf("https://pro.superlib.com/web/teacher/info") > -1
    ) {
      _url = decodeURIComponent(options.url + "?tid=" + options.tid);
    } else {
      _url = decodeURIComponent(options.url);
    }
    console.log(_url)
    this.setData({
      url: _url
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log("link hide")
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function (options) {
    console.log("link 卸载")
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    let url = util.getCurrentPageUrlWithArgs();
    if(url.indexOf('?') > -1){
      url += '&fid='+app.globalData.fid+'&fidEnc='+app.globalData.fidEnc;
    }else{
      url += '&fid='+app.globalData.fid+'&fidEnc='+app.globalData.fidEnc;
    }
    if(options.from == "menu"){
      return {
        title:app.globalData.site.name,
        path: url
      }
    }
  }
})